<template>
    <div class="app-container">
      <!-- 新增按钮 -->
      <div style="margin-bottom: 20px;">
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </div>
  
      <!-- 搜索表单 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="分类">
          <el-input v-model="searchForm.category" placeholder="请输入分类"></el-input>
        </el-form-item>
        <el-form-item label="所属店铺">
          <el-input v-model="searchForm.affiliation" placeholder="请输入所属店铺"></el-input>
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select v-model="searchForm.status" placeholder="请选择审核状态">
            <el-option label="全部" value=""></el-option>
            <el-option label="通过" value="approved"></el-option>
            <el-option label="未通过" value="rejected"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
  
      <!-- 数据表格 -->
      <el-table :data="tableData" border style="width: 100%" row-key="id">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="category" label="分类"></el-table-column>
        <el-table-column prop="description" label="描述"></el-table-column>
        <el-table-column prop="affiliation" label="所属店铺"></el-table-column>
        <el-table-column prop="status" label="审核状态">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status === 'approved' ? 'success' : 'danger'">
              {{ scope.row.status === 'approved' ? '通过' : '未通过' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="availability" label="是否可用">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.availability" active-value="Y" inactive-value="N"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <!-- 分页 -->
      <el-pagination
        background
        layout="prev, pager, next, sizes, total"
        :total="total"
        :page-size="pageSize"
        :current-page="pageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
  
      <!-- 新增/编辑对话框 -->
      <el-dialog title="分类信息" :visible.sync="dialogVisible">
        <el-form :model="form" label-width="100px">
          <el-form-item label="分类">
            <el-input v-model="form.category"></el-input>
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="form.description"></el-input>
          </el-form-item>
          <el-form-item label="所属店铺">
            <el-input v-model="form.affiliation"></el-input>
          </el-form-item>
          <el-form-item label="审核状态">
            <el-select v-model="form.status" placeholder="请选择审核状态">
              <el-option label="通过" value="approved"></el-option>
              <el-option label="未通过" value="rejected"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否可用">
            <el-switch v-model="form.availability" active-value="Y" inactive-value="N"></el-switch>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveData">保存</el-button>
        </span>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { getList, addCategory, updateCategory, deleteCategory } from "@/api/productCategory";
  
  export default {
    data() {
      return {
        searchForm: {
          category: "",
          affiliation: "",
          status: ""
        },
        tableData: [],
        total: 0,
        pageNum: 1,
        pageSize: 10,
        dialogVisible: false,
        form: {
          id: null,
          category: "",
          description: "",
          affiliation: "",
          status: "",
          availability: "Y"
        }
      };
    },
    created() {
      this.fetchData();
    },
    methods: {
      // 获取数据
      fetchData() {
        const params = {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          id: this.searchForm.id || null, // 分类 ID
          category: this.searchForm.category || null, // 分类名称
          description: this.searchForm.description || null, // 描述
          affiliation: this.searchForm.affiliation || null, // 所属店铺
          status: this.searchForm.status || null, // 审核状态
          time: this.searchForm.time || null // 时间范围
        };
        getList(params).then((response) => {
          this.tableData = response.data.searchInfo.map(item => {
            return {
              ...item,
              createTime: this.formatDate(item.createTime)
            };
          });
          this.total = response.data.total;
        });
      },
      // 搜索
      handleSearch() {
        this.pageNum = 1;
        this.fetchData();
      },
      // 重置搜索
      resetSearch() {
        this.searchForm = {
          category: "",
          affiliation: "",
          status: ""
        };
        this.handleSearch();
      },
      // 分页
      handleSizeChange(size) {
        this.pageSize = size;
        this.fetchData();
      },
      handleCurrentChange(page) {
        this.pageNum = page;
        this.fetchData();
      },
      // 新增
      handleAdd() {
        this.dialogVisible = true;
        this.form = {
          id: null,
          category: "",
          description: "",
          affiliation: "",
          status: "",
          availability: "Y"
        };
      },
      // 编辑
      handleEdit(row) {
        this.dialogVisible = true;
        this.form = { ...row };
      },
      // 保存
      saveData() {
    
        if (this.form.id) {
          updateCategory(this.form).then(() => {
            this.$message.success("更新成功");
            this.dialogVisible = false;
            this.fetchData();
          });
        } else {
          addCategory(this.form).then(() => {
            this.$message.success("新增成功");
            this.dialogVisible = false;
            this.fetchData();
          });
        }
      },
      // 删除
      handleDelete(row) {
        this.$confirm("确定删除该分类吗？", "提示", {
          type: "warning"
        }).then(() => {
          deleteCategory(row.id).then(() => {
            this.$message.success("删除成功");
            this.fetchData();
          });
        });
      },
      // 格式化日期
      formatDate(dateStr) {
        const date = new Date(dateStr);
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      }
    }
  };
  </script>
  
  <style scoped>
  .app-container {
    padding: 20px;
  }
  </style>    